import { getLevelsSuccess, getLevelsFailure } from '../actions/levelsActions'
import { FETCH_LEVELS_REQUEST } from '../types/levelsTypes'

import { api } from '../services/levelsService'

function* getLevels() {
  try {
    const levels = yield ReduxSaga.effects.call(api.getLevels)
    yield ReduxSaga.effects.put(getLevelsSuccess(levels))
  } catch (error) {
    yield ReduxSaga.effects.put(getLevelsFailure(error))
  }
}

function* watchGetLevels() {
  yield ReduxSaga.effects.takeEvery(FETCH_LEVELS_REQUEST, getLevels)
}

export default function* levelSaga() {
  yield watchGetLevels()
}